package com.bstek.demo.urule.cluster;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

/**
 * Redis配置
 */
@Configuration
public class RedisConfig {

    @Bean("redisTemplate")
    @ConfigurationProperties(prefix = "spring.redis")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //将key的序列化设置成StringRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        // 将java对象转换为 JSON 格式的字符串
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean("redisTemplate2")
    public RedisTemplate<String, byte[]> redisTemplate2(RedisConnectionFactory factory) {
        RedisTemplate<String, byte[]> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //将key的序列化设置成StringRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        // 使用 Java 内置的序列化机制将对象转换为字节数组。存储在 Redis 中的数据是二进制格式，不可读
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    public class CustomValueSerializer implements RedisSerializer<Object> {

        private final GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
        private final JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();

        @Override
        public byte[] serialize(Object object) throws SerializationException {
            if (object instanceof byte[]) {
                return (byte[]) object;
            } else if (object instanceof String) {
                return jsonSerializer.serialize(object);
            } else {
                return jdkSerializer.serialize(object);
            }
        }

        @Override
        public Object deserialize(byte[] bytes) throws SerializationException {
            if (bytes == null || bytes.length == 0) {
                return null;
            }
            try {
                // 尝试直接返回 byte[]
                return bytes;
            } catch (Exception e) {
                try {
                    // 尝试 JSON 反序列化
                    return jsonSerializer.deserialize(bytes);
                } catch (Exception ex) {
                    // 尝试 JDK 反序列化
                    return jdkSerializer.deserialize(bytes);
                }
            }
        }
    }
}
